using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using  UnityEngine.Events;
public class Unit_ItemTrigger_Ctrl : Unit_Base
{
    public override UnitType unitType => UnitType.ItemTrigger;

    public string triggerTag;

    public UnityEvent<Unit_Item_Ctrl> triggerItemEvent;
    public UnityEvent<Unit_Charactor_Ctrl> triggerCharactorEvent;
    public UnityEvent<Unit_Biology_Ctrl> triggerBiologyEvent;
    public bool isMoveItem = false;
    public bool isHideItem = false;

    public bool CheckItem(Unit_Item_Ctrl unit_Item_Ctrl)
    {
        if (!string.IsNullOrEmpty(triggerTag) && (unit_Item_Ctrl.tags.Contains(triggerTag) || triggerTag == unit_Item_Ctrl.itemName) && unit_Item_Ctrl.source_Ctrl != this)
        {
            unit_Item_Ctrl.source_Ctrl = this;
            //unit_Item_Ctrl.transform.parent = this.transform;
            Vector3 initPos = unit_Item_Ctrl.transform.position;
            Vector3 handlePos = this.transform.position + Quaternion.Euler(0, 0, Random.Range(-20, 20f)) * (unit_Item_Ctrl.transform.position - this.transform.position) * 2;
            if (isMoveItem)
            {
                IEPool_Manager.instance.KeepTimeToDo("", 0.5f, null, (_time) =>
                {
                    unit_Item_Ctrl.transform.position = Bezier.GetTriangleBezierByT(initPos, handlePos, handlePos, this.transform.position, 1 - _time / 0.5f);
                    return true;
                }, () =>
                {
                    if (isHideItem)
                    {
                        unit_Item_Ctrl.gameObject.SetActive(false);
                    }
                    triggerItemEvent?.Invoke(unit_Item_Ctrl);
                });
                return true;
            }
            if (isHideItem)
            {
                unit_Item_Ctrl.gameObject.SetActive(false);
            }
            triggerItemEvent?.Invoke(unit_Item_Ctrl);
            return true;
        }
        return false;
    }
    public void OnOtherTouch(Collider2D collider2D)
    {
        Unit_Base[] unit_Ctrls = collider2D.GetComponentsInParent<Unit_Base>();
        foreach (Unit_Base unit_Ctrl in unit_Ctrls)
        {
            if (unit_Ctrl != null)
            {
                switch (unit_Ctrl.unitType)
                {
                    case UnitType.Charactor:
                        Unit_Charactor_Ctrl charactor_Ctrl = (Unit_Charactor_Ctrl)unit_Ctrl;
                        bool isCheckSuccess = false;
                        for (int i = 0; i < charactor_Ctrl.ownItems.Count; i++)
                        {
                            if (CheckItem(charactor_Ctrl.ownItems[i]))
                            {
                                isCheckSuccess = true;
                                break;
                            }
                        }
                        if (!isCheckSuccess)
                        {
                            triggerCharactorEvent?.Invoke(charactor_Ctrl);
                        }
                        break;
                    case UnitType.Item:
                        Unit_Item_Ctrl item_Ctrl = (Unit_Item_Ctrl)unit_Ctrl;
                        if (item_Ctrl.source_Ctrl == null)
                        {
                            CheckItem(item_Ctrl);
                        }
                        break;
                    case UnitType.Biology:
                        Unit_Biology_Ctrl biology_Ctrl = (Unit_Biology_Ctrl)unit_Ctrl;
                        triggerBiologyEvent?.Invoke(biology_Ctrl);
                        break;
                }
            }
        }
    }
    public void OnOtherTouch(Collision2D collision2D)
    {
        OnOtherTouch(collision2D.collider);
    }
}
